home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2001 May / SGI Freeware 2001 May - Disc 1.iso / dist / fw_mysql.idb / usr / freeware / bin / mysql_setpermission.z / mysql_setpermission
Text File  |  1999-10-18  |  15KB  |  670 lines

  1. #!/bin/perl5
  2. ## Emacs, this is -*- perl -*- mode? :-)
  3. ##
  4. ##        Permission setter for MySQL
  5. ##
  6. ##        mady by Luuk de Boer (luuk@wxs.nl) 1998.
  7. ##        it's made under GPL ...:-))
  8. ##
  9. ##
  10. ############################################################################
  11. ## History
  12. ##
  13. ## 1.0 first start of the program
  14. ## 1.1 some changes from monty and after that
  15. ##     initial release in mysql 3.22.10 (nov 1998)
  16. ## 1.2 begin screen now in a loop + quit is using 0 instead of 9
  17. ##     after ideas of Paul DuBois.
  18. ## 1.2a Add Grant, References, Index and Alter privilege handling (Monty)
  19.  
  20. #### TODO
  21. #
  22. # empty ... suggestions ... mail them to me ...
  23.  
  24.  
  25. $version="1.2";
  26.  
  27. use DBI;
  28. use Getopt::Long;
  29. use strict;
  30. use vars qw($dbh $hostname $opt_user $opt_password $opt_help $opt_host
  31.         $opt_socket $opt_port $host $version);
  32.  
  33.  
  34. $dbh=$host=$opt_user= $opt_password= $opt_help= $opt_host= $opt_socket= "";
  35. $opt_port=0;
  36.  
  37. read_my_cnf();        # Read options from ~/.my.cnf
  38.  
  39. GetOptions("user=s","password=s","help","host=s","socket=s","port=i");
  40.  
  41. usage() if ($opt_help); # the help function
  42.  
  43. if ($opt_host eq '')
  44. {
  45.   $hostname = "localhost";
  46. }
  47. else
  48. {
  49.   $hostname = $opt_host;
  50. }
  51.  
  52. # ask for a password if no password is set already
  53. if ($opt_password eq '')
  54. {
  55.   system "stty -echo";
  56.   print "Password for user $opt_user to connect to MySQL: ";
  57.   $opt_password = <STDIN>;
  58.   chomp($opt_password);
  59.   system "stty echo";
  60.   print "\n";
  61. }
  62.  
  63.  
  64. # make the connection to MySQL
  65. $dbh= DBI->connect("DBI:mysql:mysql:host=$hostname:port=$opt_port:mysql_socket=$opt_socket",$opt_user,$opt_password, {PrintError => 0}) ||
  66.   die("Can't make a connection to the mysql server.\n The error: $DBI::errstr");
  67.  
  68. # the start of the program
  69. &q1();
  70. exit(0); # the end...
  71.  
  72. #####
  73. # below all subroutines of the program
  74. #####
  75.  
  76. ###
  77. # the beginning of the program
  78. ###
  79. sub q1 { # first question ...
  80.   my ($answer,$end);
  81.   while (! $end) {
  82.     print "#"x70;
  83.     print "\n";
  84.     print "## Welcome to the permission setter $version for MySQL.\n";
  85.     print "## made by Luuk de Boer\n";
  86.     print "#"x70;
  87.     print "\n";
  88.     print "What would you like to do:\n";
  89.     print "  1. Set password for a user.\n";
  90.     print "  2. Add a database + user privilege for that database.\n";
  91.     print "     - user can do all except all admin functions\n";
  92.     print "  3. Add user privilege for an existing database.\n";
  93.     print "     - user can do all except all admin functions\n";
  94.     print "  4. Add user privilege for an existing database.\n";
  95.     print "     - user can do all except all admin functions + no create/drop\n";
  96.     print "  5. Add user privilege for an existing database.\n";
  97.     print "     - user can do only selects (no update/delete/insert etc.)\n";
  98.     print "  0. exit this program\n";
  99.     print "\nMake your choice [1,2,3,4,5,0]: ";
  100.     while (<STDIN>) {
  101.       $answer = $_;
  102.       chomp($answer);
  103.       if ($answer =~ /1|2|3|4|5|0/) {
  104.         &setpwd if ($answer == 1);
  105.         &addall($answer) if ($answer =~ /^[2345]$/);
  106.         if ($answer == 0) {
  107.           print "Sorry, hope we can help you next time \n\n";
  108.           $end = 1;
  109.         }
  110.       } else {
  111.         print "Your answer was $answer\n";
  112.         print "and that's wrong .... Try again\n";
  113.       }
  114.       last;
  115.     }
  116.   }
  117. }
  118.  
  119. ###
  120. # set a password for a user
  121. ###
  122. sub setpwd
  123. {
  124.   my ($user,$pass,$host);
  125.   print "\n\nSetting a (new) password for a user.\n";
  126.  
  127.   $user = user();
  128.   $pass = newpass($user);
  129.   $host = hosts($user);
  130.  
  131.   print "#"x70;
  132.   print "\n\n";
  133.   print "That was it ... here is an overview of what you gave to me:\n";
  134.   print "The username         : $user\n";
  135. #  print "The password        : $pass\n";
  136.   print "The host        : $host\n";
  137.   print "#"x70;
  138.   print "\n\n";
  139.   print "Are you pretty sure you would like to implement this [yes/no]: ";
  140.   my $no = <STDIN>;
  141.   chomp($no);
  142.   if ($no =~ /n/i)
  143.   {
  144.     print "Okay .. that was it then ... See ya\n\n";
  145.     return(0);
  146.   }
  147.   else
  148.   {
  149.     print "Okay ... let's go then ...\n\n";
  150.   }
  151.   $user = $dbh->quote($user);
  152.   $host = $dbh->quote($host);
  153.   if ($pass eq '')
  154.   {
  155.     $pass = "''";
  156.   }
  157.   else
  158.   {
  159.     $pass = "PASSWORD(". $dbh->quote($pass) . ")";
  160.   }
  161.   my $sth = $dbh->prepare("update user set Password=$pass where User = $user and Host = $host") || die $dbh->errstr;
  162.   $sth->execute || die $dbh->errstr;
  163.   $sth->finish;
  164.   print "The password is set for user $user.\n\n";
  165.  
  166. }
  167.  
  168. ###
  169. # all things which will be added are done here
  170. ###
  171. sub addall
  172. {
  173.   my ($todo) = @_;
  174.   my ($answer,$good,$db,$user,$pass,$host,$priv);
  175.  
  176.   if ($todo == 2)
  177.   {
  178.     $db = newdatabase();
  179.   }
  180.   else
  181.   {
  182.     $db = database();
  183.   }
  184.  
  185.   $user = newuser();
  186.   $pass = newpass($user);
  187.   $host = newhosts();
  188.  
  189.   print "#"x70;
  190.   print "\n\n";
  191.   print "That was it ... here is an overview of what you gave to me:\n";
  192.   print "The database name    : $db\n";
  193.   print "The username         : $user\n";
  194. #  print "The password        : $pass\n";
  195.   print "The host(s)        : $host\n";
  196.   print "#"x70;
  197.   print "\n\n";
  198.   print "Are you pretty sure you would like to implement this [yes/no]: ";
  199.   my $no = <STDIN>;
  200.   chomp($no);
  201.   if ($no =~ /n/i)
  202.   {
  203.     print "Okay .. that was it then ... See ya\n\n";
  204.     return(0);
  205.   }
  206.   else
  207.   {
  208.     print "Okay ... let's go then ...\n\n";
  209.   }
  210.  
  211.   if ($todo == 2)
  212.   {
  213.     # create the database
  214.     my $sth = $dbh->do("create database $db") || $dbh->errstr;
  215.   }
  216.  
  217.   # select the privilege ....
  218.   if (($todo == 2) || ($todo == 3))
  219.   {
  220.     $priv = "'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y'";
  221.   }
  222.   elsif ($todo == 4)
  223.   {
  224.     $priv = "'Y','Y','Y','Y','N','N','N','Y','Y','Y'";
  225.   }
  226.   elsif ($todo == 5)
  227.   {
  228.     $priv = "'Y','N','N','N','N','N','N','N','N','N'";
  229.   }
  230.   else
  231.   {
  232.     print "Sorry, choice  number $todo isn't known inside the program .. See ya\n";
  233.     quit();
  234.   }
  235.  
  236.   my @hosts = split(/,/,$host);
  237.   $user = $dbh->quote($user);
  238.   $db = $dbh->quote($db);
  239.   if ($pass eq '')
  240.   {
  241.     $pass = "''";
  242.   }
  243.   else
  244.   {
  245.     $pass = "PASSWORD(". $dbh->quote($pass) . ")";
  246.   }
  247.   foreach my $key (@hosts)
  248.   {
  249.     my $key1 = $dbh->quote($key);
  250.     my $sth = $dbh->prepare("select Host,User from user where Host = $key1 and User = $user") || die $dbh->errstr;
  251.     $sth->execute || die $dbh->errstr;
  252.     my @r = $sth->fetchrow_array;
  253.     if ($r[0])
  254.     {
  255.       print "WARNING WARNING SKIPPING CREATE FOR USER $user AND HOST $key\n";
  256.       print "Reason: entry already exists in the user table.\n";
  257.     }
  258.     else
  259.     {
  260.       $sth = $dbh->prepare("insert into user (Host,User,Password) values($key1,$user,$pass)") || die $dbh->errstr;
  261.       $sth->execute || die $dbh->errstr;
  262.       $sth->finish;
  263.     }
  264.     $sth = $dbh->prepare("INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Grant_priv,References_priv,Index_priv,Alter_priv) VALUES ($key1,$db,$user,$priv)") || die $dbh->errstr;
  265.     $sth->execute || die $dbh->errstr;
  266.     $sth->finish;
  267.   }
  268.   $dbh->do("flush privileges") || print "Can't load privileges\n";
  269.   print "Everything is inserted and mysql privileges have been reloaded.\n\n";
  270. }
  271.  
  272. ###
  273. # ask for a new database name
  274. ###
  275. sub newdatabase
  276. {
  277.   my ($answer,$good,$db);
  278.   print "\n\nWhich database would you like to add: ";
  279.   while (<STDIN>)
  280.   {
  281.     $answer = $_;
  282.     $good = 0;
  283.     chomp($answer);
  284.     if ($answer)
  285.     {
  286.       my $sth = $dbh->prepare("show databases") || die $dbh->errstr;
  287.       $sth->execute || die $dbh->errstr;
  288.       while (my @r = $sth->fetchrow_array)
  289.       {
  290.         if ($r[0] eq $answer)
  291.     {
  292.           print "\n\nSorry, this database name is already in use; try something else: ";
  293.           $good = 1;
  294.         }
  295.       }
  296.     }
  297.     else
  298.     {
  299.       print "You must type something ...\nTry again: ";
  300.       next;
  301.     }
  302.     last if ($good == 0);
  303.   }
  304.   $db = $answer;
  305.   print "The new database $db will be created\n";
  306.   return($db);
  307. }
  308.  
  309. ###
  310. # select a database
  311. ###
  312. sub database
  313. {
  314.   my ($answer,$good,$db);
  315.   print "\n\nWhich database would you like to select: \n";
  316.   print "You can choose from: \n";
  317.   my $sth = $dbh->prepare("show databases") || die $dbh->errstr;
  318.   $sth->execute || die $dbh->errstr;
  319.   while (my @r = $sth->fetchrow_array)
  320.   {
  321.     print "  - $r[0] \n";
  322.   }
  323.   print "Which database will it be (case sensitive): ";
  324.   while (<STDIN>)
  325.   {
  326.     $answer = $_;
  327.     $good = 0;
  328.     chomp($answer);
  329.     if ($answer)
  330.     {
  331.       my $sth = $dbh->prepare("show databases") || die $dbh->errstr;
  332.       $sth->execute || die $dbh->errstr;
  333.       while (my @r = $sth->fetchrow_array)
  334.       {
  335.         if ($r[0] eq $answer)
  336.     {
  337.           $good = 1;
  338.           $db = $r[0];
  339.           last;
  340.         }
  341.       }
  342.     }
  343.     else
  344.     {
  345.       print "You must type something ...\nTry again: ";
  346.       next;
  347.     }
  348.     if ($good == 1)
  349.     {
  350.       last;
  351.     }
  352.     else
  353.     {
  354.       print "You must select one from the list.\nTry again: ";
  355.       next;
  356.     }
  357.   }
  358.   print "The database $db will be used.\n";
  359.   return($db);
  360. }
  361.  
  362. ###
  363. # ask for a new username
  364. ###
  365. sub newuser
  366. {
  367.   my ($answer,$user);
  368.  
  369.   print "\nWhat username is to be created: ";
  370.   while(<STDIN>)
  371.   {
  372.     $answer = $_;
  373.     chomp($answer);
  374.     if ($answer)
  375.     {
  376.       $user = $answer;
  377.     }
  378.     else
  379.     {
  380.       print "You must type something ...\nTry again: ";
  381.       next;
  382.     }
  383.     last;
  384.   }
  385.   print "Username = $user\n";
  386.   return($user);
  387. }
  388.  
  389. ###
  390. # ask for a user which is already in the user table
  391. ###
  392. sub user
  393. {
  394.   my ($answer,$user);
  395.  
  396.   print "\nFor which user do you want to specify a password: ";
  397.   while(<STDIN>)
  398.   {
  399.     $answer = $_;
  400.     chomp($answer);
  401.     if ($answer)
  402.     {
  403.       my $sth = $dbh->prepare("select User from user where User = '$answer'") || die $dbh->errstr;
  404.       $sth->execute || die $dbh->errstr;
  405.       my @r = $sth->fetchrow_array;
  406.       if ($r[0])
  407.       {
  408.         $user = $r[0];
  409.       }
  410.       else
  411.       {
  412.        print "Sorry, user $answer isn't known in the user table.\nTry again: ";
  413.        next;
  414.      }
  415.     }
  416.     else
  417.     {
  418.       print "You must type something ...\nTry again: ";
  419.       next;
  420.     }
  421.     last;
  422.   }
  423.   print "Username = $user\n";
  424.   return($user);
  425. }
  426.  
  427. ###
  428. # ask for a new password
  429. ###
  430. sub newpass
  431. {
  432.   my ($user) = @_;
  433.   my ($answer,$good,$pass,$yes);
  434.  
  435.   print "Would you like to set a password for $user [y/n]: ";
  436.   $yes = <STDIN>;
  437.   chomp($yes);
  438.   if ($yes =~ /y/)
  439.   {
  440.     system "stty -echo";
  441.     print "What password do you want to specify for $user: ";
  442.     while(<STDIN>)
  443.     {
  444.       $answer = $_;
  445.       chomp($answer);
  446.       system "stty echo";
  447.       print "\n";
  448.       if ($answer)
  449.       {
  450.         system "stty -echo";
  451.         print "Type the password again: ";
  452.         my $second = <STDIN>;
  453.         chomp($second);
  454.         system "stty echo";
  455.         print "\n";
  456.         if ($answer ne $second)
  457.         {
  458.           print "Passwords aren't the same; we begin from scratch again.\n";
  459.           system "stty -echo";
  460.           print "Password please: ";
  461.           next;
  462.         }
  463.         else
  464.         {
  465.           $pass = $answer;
  466.         }
  467.       }
  468.       else
  469.       {
  470.         print "You must type something ...\nTry again: ";
  471.         next;
  472.       }
  473.       last;
  474.     }
  475. #    print "The password for $user is $pass.\n";
  476.   }
  477.   else
  478.   {
  479.     print "We won't set a password so the user doesn't have to use it\n";
  480.     $pass = "";
  481.   }
  482.   return($pass);
  483. }
  484.  
  485. ###
  486. # ask for new hosts
  487. ###
  488. sub newhosts
  489. {
  490.   my ($answer,$good,$host);
  491.  
  492.   print "We now need to know from what host(s) the user will connect.\n";
  493.   print "Keep in mind that % means 'from any host' ...\n";
  494.   print "The host please: ";
  495.   while(<STDIN>)
  496.   {
  497.     $answer = $_;
  498.     chomp($answer);
  499.     if ($answer)
  500.     {
  501.       $host .= ",$answer";
  502.       print "Would you like to add another host [yes/no]: ";
  503.       my $yes = <STDIN>;
  504.       chomp($yes);
  505.       if ($yes =~ /y/i)
  506.       {
  507.         print "Okay, give us the host please: ";
  508.         next;
  509.       }
  510.       else
  511.       {
  512.         print "Okay we keep it with this ...\n";
  513.       }
  514.     }
  515.     else
  516.     {
  517.       print "You must type something ...\nTry again: ";
  518.       next;
  519.     }
  520.     last;
  521.   }
  522.   $host =~ s/^,//;
  523.   print "The following host(s) will be used: $host.\n";
  524.   return($host);
  525. }
  526.  
  527. ###
  528. # ask for a host which is already in the user table
  529. ###
  530. sub hosts
  531. {
  532.   my ($user) = @_;
  533.   my ($answer,$good,$host);
  534.  
  535.   print "We now need to know which host for $user we have to change.\n";
  536.   print "Choose from the following hosts: \n";
  537.   $user = $dbh->quote($user);
  538.   my $sth = $dbh->prepare("select Host,User from user where User = $user") || die $dbh->errstr;
  539.   $sth->execute || die $dbh->errstr;
  540.   while (my @r = $sth->fetchrow_array)
  541.   {
  542.     print "  - $r[0] \n";
  543.   }
  544.   print "The host please (case sensitive): ";
  545.   while(<STDIN>)
  546.   {
  547.     $answer = $_;
  548.     chomp($answer);
  549.     if ($answer)
  550.     {
  551.       $sth = $dbh->prepare("select Host,User from user where Host = '$answer' and User = $user") || die $dbh->errstr;
  552.       $sth->execute || die $dbh->errstr;
  553.       my @r = $sth->fetchrow_array;
  554.       if ($r[0])
  555.       {
  556.         $host = $answer;
  557.         last;
  558.       }
  559.       else
  560.       {
  561.         print "You have to select a host from the list ...\nTry again: ";
  562.         next;
  563.       }
  564.     }
  565.     else
  566.     {
  567.       print "You have to type something ...\nTry again: ";
  568.       next;
  569.     }
  570.     last;
  571.   }
  572.   print "The following host will be used: $host.\n";
  573.   return($host);
  574. }
  575.  
  576. ###
  577. # a nice quit (first disconnect and then exit
  578. ###
  579. sub quit
  580. {
  581.   $dbh->disconnect;
  582.   exit(0);
  583. }
  584.  
  585. ###
  586. # Read variables password, port and socket from .my.cnf under the client
  587. # or perl groups
  588. ###
  589.  
  590. sub read_my_cnf
  591. {
  592.   open(TMP,$ENV{'HOME'} . "/.my.cnf") || return 1;
  593.   while (<TMP>)
  594.   {
  595.     if (/^\[(client|perl)\]/i)
  596.     {
  597.       while ((defined($_=<TMP>)) && !/^\[\w+\]/)
  598.       {
  599.     print $_;
  600.     if (/^host\s*=\s*(\S+)/i)
  601.     {
  602.       $opt_host = $1;
  603.     }
  604.     elsif (/^user\s*=\s*(\S+)/i)
  605.     {
  606.       $opt_user = $1;
  607.     }
  608.     elsif (/^password\s*=\s*(\S+)/i)
  609.     {
  610.       $opt_password = $1;
  611.     }
  612.     elsif (/^port\s*=\s*(\S+)/i)
  613.     {
  614.       $opt_port = $1;
  615.     }
  616.     elsif (/^socket\s*=\s*(\S+)/i)
  617.     {
  618.       $opt_socket = $1;
  619.     }
  620.       }
  621.     }
  622.   }
  623.   close(TMP);
  624. }
  625.  
  626. ###
  627. # the help text
  628. ###
  629. sub usage
  630. {
  631.   print <<EOL;
  632. ----------------------------------------------------------------------
  633.                  The permission setter for MySQL.
  634.                       version: $version
  635.  
  636.                  made by: Luuk de Boer <luuk\@wxs.nl>
  637. ----------------------------------------------------------------------
  638.  
  639. The permission setter is a little program which can help you add users
  640. or databases or change passwords in MySQL. Keep in mind that we don't
  641. check permissions which already been set in MySQL. So if you can't
  642. connect to MySQL using the permission you just added, take a look at
  643. the permissions which have already been set in MySQL.
  644.  
  645. The permission setter first reads your .my.cnf file in your Home
  646. directory if it exists.
  647.  
  648. Options for the permission setter:
  649.  
  650. --help        : print this help message and exit.
  651.  
  652. The options shown below are used for making the connection to the MySQL
  653. server. Keep in mind that the permissions for the user specified via
  654. these options must be sufficient to add users / create databases / set
  655. passwords.
  656.  
  657. --user        : is the username to connect with.
  658. --password    : the password of the username.
  659. --host        : the host to connect to.
  660. --socket    : the socket to connect to.
  661. --port        : the port number of the host to connect to.
  662.  
  663. If you don't give a password and no password is set in your .my.cnf
  664. file, then the permission setter will ask for a password.
  665.  
  666.  
  667. EOL
  668. exit(0);
  669. }
  670.